package interview9;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author Jack
 * @date 2019-11-06-14:48
 * 用两个队列实现栈
 */
public class Solution2 {

    private Queue queue1 = new LinkedBlockingQueue();
    private Queue queue2 = new LinkedBlockingDeque();

    public void appendTail(Object obj) throws Exception {
        if(obj == null){
            throw new Exception("请输入合法的值");
        }
        if(queue1.size() == 0 && queue2.size() == 0){
            queue1.add(obj);
            return ;
        }
        if(queue1.size() != 0){
            queue1.add(obj);
            return ;
        }
        if(queue2.size() != 0){
            queue2.add(obj);
            return ;
        }
    }

    public Object deleteHead() throws Exception {
        Object obj;
        if(queue1.size() != 0){
            while (queue1.size() != 1){
                obj = queue1.poll();
                queue2.add(obj);
            }
            return queue1.poll();
        }
        if(queue2.size() != 0){
            while (queue2.size() != 1){
                obj = queue2.poll();
                queue1.add(obj);
            }
            return queue2.poll();
        }
        throw new Exception("栈为空");
    }
}
